package mt521.cam.ac.uk;

public class TESTSFrequencyGrabber {
    
    static double sampleRate = Encode.getSampleRate();

    public static void main(String[] args) {
        /*int freq = 2670;
        System.out.println("Frequency in: "+freq);
        double[] array = Decode.fft(makeArray(freq,32));
        double answer = getFrequency(array);
        long solution = 30*(Math.round(answer/30));
        System.out.println("Frequency returned: "+solution);
        System.out.println("Byte Returned: "+solution/30);*/
        //koala();
        
        byte[] bTone = Encode.makeTone("750");
        double[] dTone = new double[bTone.length];
        for (int i=0; i<bTone.length; i++) {
            dTone[i] = bTone[i];
        }
        double[] transformed = Decode.fft(dTone);
        System.out.println("Frequency Detected: "+Decode.getFrequency(transformed));
        Graph.graph(transformed);
    }
    
   /* public static void koala() {
        int freq;
        long solution;
        byte[] bArray;
        double answer;
        for (freq=0; freq<7680; freq+=30) {
            bArray = Encode.buffer(freq,32);
            answer = Decode.decode(bArray);
            solution = 15*(Math.round(answer/15));
            System.out.print((solution/30)+" ");
        }
    }

    public static double[] makeArray(int freq, int msecs) {
        double[] sineWave = new double[(int) ((sampleRate*msecs)/1000)];
        double freqRatio = (sampleRate)/freq; //allow for mismatch in sample rate and frequency (both measured in Hz)
        for (int i=0; i<sineWave.length; i++) {
            double angle = i/freqRatio * 2.0 * Math.PI; //radians, multiply by 2PI as going to be used in sine wave
            sineWave[i] = (Math.sin(angle) * 127.0);
            //sine of an angle gives ~1->1, we want a number in the range ~128->127 (8 bits), so multiply by
            //127 to use all byte values.
        }
        return sineWave;
    }
   
    public static double getFrequency(double[] array) {
        int bin = 0;
        for (int i=0; i<array.length/2; i++) {
            bin = array[i]>array[bin] ? i : bin;
        }
        double frequency = ((double)(bin*sampleRate))/array.length;
        return frequency;
    }
*/
}
